<?php
/**
 * 门票规格
 * @copyright  Copyright (c) 2015-2025 NcPHP Inc. (http://www.ncphp.com)
 * @license    http://www.ncphp.com/
 * @link       http://www.ncphp.com/
 * @author	   NcPHP Team
 * @since      File available since Release v2.1
 */
defined('InNcPHP') or exit('Access Invalid!');

class ticket_typeControl extends SystemControl{
	private $links = array(
		array('url'=>'act=ticket_type&op=index','lang'=>'nc_manage'),
		array('url'=>'act=ticket_type&op=add','lang'=>'nc_new'),
	);

	public function __construct(){
		parent::__construct();
		Language::read('ticket_type');
	}

	/**
	 * 管理
	 */
	public function indexOp(){
		/* @var ticket_typeModel $model */
		$model = Model('ticket_type');

		//列表
		$lists = $model->getTicketTypePage();

        Tpl::output('lists', $lists);
        Tpl::output('page',$model->showpage());
        Tpl::output('top_link', $this->sublink($this->links,'index'));
        Tpl::showpage('ticket_type.index');
	}

	/**
	 * 添加
	 */
	public function addOp(){
		$lang	= Language::getLangContent();
        /* @var ticket_typeModel $model */
        $model = Model('ticket_type');
		if (chksubmit()){
		    $params = array(
		        'type_name' => trim($_POST['type_name']),
                'type_desc' => trim($_POST['type_desc'])
            );
		    $res = $model->addTicketType($params);
            if ($res){
                $url = array(
                    array(
                        'url'=>'index.php?act=ticket_type&op=index',
                        'msg'=>$lang['type_back_list'],
                    )
                );
                $this->log(L('nc_add,type_index').'['.$_POST['type_name'].']',1);
                showMessage($lang['nc_common_save_succ'],$url);
            }else {
                $this->log(L('nc_add,type_index').'['.$_POST['type_name'].']',0);
                showMessage($lang['nc_common_save_fail']);
            }
		}

		Tpl::output('top_link',$this->sublink($this->links,'add'));
		Tpl::showpage('ticket_type.add');
	}

    /**
     * 字段校验
     */
	public function checkOp()
    {
        /* @var ticket_typeModel $model */
        $model = Model('ticket_type');

        $condition = array();
        if (isset($_GET['id'])) {
            $condition['type_id'] = array('neq', intval($_GET['id']));
        }
        $condition['type_name'] = trim($_GET['type_name']);
        $info = $model->getTicketTypeInfo($condition);
        exit($info ? 'false' : 'true');
    }
	
	/**
	 * 编辑
	 */
	public function editOp(){
		$lang	= Language::getLangContent();
        /* @var ticket_typeModel $model */
        $model = Model('ticket_type');

		if (chksubmit()){
            $params = array(
                'type_name' => trim($_POST['type_name']),
                'type_desc' => trim($_POST['type_desc'])
            );
            $condition = array('type_id'=>intval($_POST['id']));
            $res = $model->updateTicketType($condition, $params);
            if ($res){
                $url = array(
                    array(
                        'url'=>'index.php?act=ticket_type&op=index',
                        'msg'=>$lang['type_back_list'],
                    )
                );
                $this->log(L('nc_edit,type_index').'['.$_POST['type_name'].']',1);
                showMessage($lang['nc_common_save_succ'],$url);
            }else {
                $this->log(L('nc_edit,type_index').'['.$_POST['type_name'].']',0);
                showMessage($lang['nc_common_save_fail']);
            }
		}

        $id = intval($_GET['id']);
        $type = $model->getTicketType($id);
        Tpl::output('type', $type);
		Tpl::showpage('ticket_type.edit');
	}

    /**
     * 删除规格
     */
    public function delOp()
    {
        $lang	= Language::getLangContent();
        /* @var ticket_typeModel $model */
        $model = Model('ticket_type');

        /* @var  ticket_skuModel $sku_model*/
        $id = intval($_GET['id']);
        $sku_model = Model('ticket_sku');
        if($id>0){
            $res = $model->delTicketType(array('type_id'=>$id));
            $re = $sku_model->delTicketSku(array('type_id'=>$id));
            if($res && $re){
                $this->log('删除票券类型'. '[id:' . $id . ']',1);
                showMessage($lang['nc_common_del_succ'],'index.php?act=ticket_type&op=index');
            }else{
                showMessage($lang['nc_common_del_fail'],'index.php?act=ticket_type&op=index');
            }
        }
    }
}
